﻿using System;
using System.Collections.Generic;
using System.Linq;
using GTAuto.DAL.Entities;
using GTAuto.DAL.Enums;
using GTAuto.DAL.Extensions;

namespace GTAuto.DAL.DAL
{
    public class DALModificationProperty : DALBase
    {
        public static DALModificationProperty Singleton = new DALModificationProperty();

        public GTModificationProperty Add(GTModificationProperty gtModificationProperty)
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {
                var modProp = new ModificationProperty();
                modProp.Active = gtModificationProperty.Active;
                modProp.Category = gtModificationProperty.Category;
                modProp.Name = gtModificationProperty.Name;
                modProp.DefaultValue = gtModificationProperty.DefaultValue;
                modProp.Type = gtModificationProperty.Type;

                db.ModificationProperties.InsertOnSubmit(modProp);
                db.SubmitChanges();
                gtModificationProperty.Id = modProp.Id;
                return gtModificationProperty;
            }
        }

        public GTModificationProperty Update(GTModificationProperty gtModificationProperty)
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {

                var prop = db.ModificationProperties.SingleOrDefault(x => x.Id == gtModificationProperty.Id);
                prop.Active = gtModificationProperty.Active;
                prop.Category = gtModificationProperty.Category;
                prop.Name = gtModificationProperty.Name;
                prop.DefaultValue = gtModificationProperty.DefaultValue;
                prop.Type = gtModificationProperty.Type;

                db.SubmitChanges();
                return gtModificationProperty;
            }
        }


        public GTModificationProperty Get(int id)
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {
                var brand = db.ModificationProperties.SingleOrDefault(x => x.Id == id);
                GTModificationProperty gtBrand = new GTModificationProperty();
                gtBrand.UpdateTo(brand);
                return gtBrand;
            }
        }

        public List<GTModificationProperty> GetByCategory(int categoryId)
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {

                var props =
                 from p in db.ModificationProperties
                 where p.Category == categoryId
                 orderby p.Name
                 select p;


                var properties = props.ToList();

                var ret = new List<GTModificationProperty>();

                foreach (var brand in properties)
                {
                    var gtBrand = new GTModificationProperty();
                    gtBrand.UpdateTo(brand);
                    ret.Add(gtBrand);
                }
                return ret;
            }
        }

        public List<GTModificationProperty> GetAll()
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {

                var props =
                 from p in db.ModificationProperties
                 orderby p.Name
                 select p;

                var properties = props.ToList();

                var ret = new List<GTModificationProperty>();

                foreach (var brand in properties)
                {
                    var gtBrand = new GTModificationProperty();
                    gtBrand.UpdateTo(brand);
                    ret.Add(gtBrand);
                }
                return ret;
            }
        }




        public void Delete(int id)
        {
            using (var db = new GTAutoDataClassesDataContext(ConnectionString))
            {
                ModificationProperty singleOrDefault = db.ModificationProperties.SingleOrDefault(x => x.Id == id);

                if (singleOrDefault != null)
                {
                    db.ModificationProperties.DeleteOnSubmit(singleOrDefault);
                    db.SubmitChanges();
                }
            }
        }





    }
}
